package com.zhu.transaction.controller;


import com.zhu.transaction.pojo.User;
import com.zhu.transaction.pojo.state.BackState;
import com.zhu.transaction.pojo.state.R;
import com.zhu.transaction.service.imp.UserServiceImp;
import com.zhu.transaction.utils.MyUtils;
import com.zhu.transaction.utils.UploadImgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2021-01-07
 */
@Controller
@RequestMapping("/user")
@Api(value = "用户接口", description = "用户的登录、注册、注销")
public class UserController {

    @Autowired
    UserServiceImp userService;

    @RequestMapping("/register")
    @ApiOperation(value = "用户注册", hidden = true)
    public String register(User user, HttpSession session) {
        BackState result = userService.register(user);
        if (result == BackState.HAS_EXIST) {
            session.setAttribute("message", "用户已存在");
            //信息存放3秒后，自动过期
            session.setMaxInactiveInterval(3);
        }
        return result == BackState.SUCCESS ? "redirect:/login" : "redirect:/user/registerOrForget?isRegister=true";
    }

    @RequestMapping("/login")
    @ApiOperation(value = "用户登录", hidden = true)
    public String login(String username, String password, HttpSession session) {
        User user = userService.login(username, password);
        if (user == null) {
            session.setAttribute("message", "用户名或密码错误");
            //信息存放3秒后，自动过期
            session.setMaxInactiveInterval(3);
            return "redirect:/login";
        } else {
            if (user.getAccountState() == 1) {
                session.setAttribute("message", "该账号被冻结");
                //信息存放3秒后，自动过期
                session.setMaxInactiveInterval(3);
                return "redirect:/login";
            }
            //登录成功，将用户信息存入session中
            session.setAttribute("user", user);
            return "redirect:/product/list";
        }
    }


    @RequestMapping("/registerOrForget")
    @ApiOperation(value = "注册或找回密码页面", hidden = true)
    public String toRegisterOrForget(Boolean isRegister, Model model) {
        model.addAttribute("isRegister", isRegister);
        return "register";
    }

    @RequestMapping("/exit")
    @ApiOperation(value = "用户退出", hidden = true)
    public String exit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/product/list";
    }

    @ResponseBody
    @PostMapping("/forget")
    @ApiOperation(value = "忘记密码", hidden = true)
    public R forget(String phone, String password) {
        User user = new User();
        user.setPhone(phone).setPassword(MyUtils.string2MD5(password)).setId(userService.getOneUserByPhone(phone).getId());
        return userService.updateUserInfo(user) > 0 ? R.ok().message("修改成功") : R.error().message("修改失败");
    }

    @ResponseBody
    @PostMapping("/getQuestion")
    @ApiOperation(value = "获取用户自定的问题")
    public R getQuestion(String phone) {
        User user = userService.getOneUserByPhone(phone);
        return user == null ? R.error().message("此用户不存在") : R.ok().data("user", user);
    }


    @ResponseBody
    @PostMapping("/upload")
    @ApiOperation(value = "用户头像上传")
    public R uploadImg(MultipartFile file, HttpSession session) {
        User user = (User) session.getAttribute("user");
        String url = UploadImgUtils.uploadImg(file, "avatar/" + user.getPhone(), MyUtils.getRandomID());
        return R.ok().data("imgUrl", url);
    }

}

